package com.pshcp.doctor.tool;

import com.framework.exception.BizException;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.DoctorLevelEnum;
import com.pshcp.common.tool.PhotoTools;
import com.pshcp.doctor.vo.personalinformation.DoctorHeadPhotoVO;
import com.pshcp.doctor.vo.personalinformation.UserProfileSummaryDataVO;
import com.pshcp.doctor.vo.personalinformation.UserProfileVO;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.DoctorDept;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.ReactiveTransaction;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 09:53
 */
public class DoctorPersonalInformationTool {

    public static UserProfileVO buildUserProfileVO(String fileUrl, Doctor doctor,List<Dept> deptList) {
        UserProfileVO userProfileVO = new UserProfileVO();
        //默认已认证
        userProfileVO.setAuth(true);
        userProfileVO.setName(doctor.getName());
        userProfileVO.setPhone(doctor.getPhone());
        userProfileVO.setFileUrl(fileUrl);
        userProfileVO.setHeadPhotoPath(doctor.getHeadPhoto());
        userProfileVO.setFullHeadPhotoPath(PhotoTools.getFullPath(fileUrl, doctor.getHeadPhoto()));
        if (doctor.getMediLevel() != null) {
            for (DoctorLevelEnum levelEnum : DoctorLevelEnum.values()) {
                if (levelEnum.getCode().intValue() == doctor.getMediLevel()) {
                    userProfileVO.setJobTitle(levelEnum.getDesc());
                    break;
                }
            }
        } else {
            userProfileVO.setJobTitle("未知");
        }
        // 处理科室信息
        List<String> deptNames = deptList.stream().map(Dept::getHisDeptName).collect(Collectors.toList());
        List<String> hisDeptIds = deptList.stream().map(Dept::getHisDeptId).collect(Collectors.toList());
        userProfileVO.setDeptName(CollectionUtils.isEmpty(deptNames)?null:StringUtils.join(deptNames,","));
        userProfileVO.setHisDeptId(CollectionUtils.isEmpty(hisDeptIds) ? null : StringUtils.join(hisDeptIds,","));

        userProfileVO.setHospitalName("中国医学科学院整形外科医院");
        userProfileVO.setReusmeFlag(StringUtils.isBlank(doctor.getReusme()) ? 0 : 1);
        userProfileVO.setGoodAtFlag(StringUtils.isBlank(doctor.getGoodAt()) ? 0 : 1);
        return userProfileVO;
    }

    public static DoctorHeadPhotoVO buildDoctorHeadPhoto(String fileUrl, Doctor doctor) {
        DoctorHeadPhotoVO doctorHeadPhotoVO = new DoctorHeadPhotoVO();
        doctorHeadPhotoVO.setFileUrl(fileUrl);
        doctorHeadPhotoVO.setHeadPhotoPath(doctor.getHeadPhoto());
        doctorHeadPhotoVO.setFullHeadPhotoPath(PhotoTools.getFullPath(fileUrl, doctor.getHeadPhoto()));
        return doctorHeadPhotoVO;
    }

    public static Doctor buildSaveDoctorHeadPhoto(String headPhotoPath, Doctor doctor) {
        doctor.setHeadPhoto(headPhotoPath);
        return doctor;
    }

    public static Doctor buildSaveDoctorReusme(String reusme, Doctor doctor) {
        doctor.setReusme(reusme);
        return doctor;
    }

    public static String buildDoctorGoodAt(String goodAt) throws BizException {
        CommonTool.checkString(goodAt, "10100003");
        List<String> goodAtStrList = Arrays.asList(goodAt.split(","));
        if (CollectionUtils.isEmpty(goodAtStrList)) {
            CommonTool.throwBizException("10100003");
        }
        List<String> goodAtList = new ArrayList<>();
        for (String gat : goodAtStrList) {
            if (StringUtils.isBlank(gat)) {
                continue;
            }
            if (gat.length() > 8) {
                CommonTool.throwBizException("10100004");
            }
            goodAtList.add(gat);
        }
        if (CollectionUtils.isEmpty(goodAtList)) {
            CommonTool.throwBizException("10100003");
        }
        if (goodAtList.size() > 5) {
            CommonTool.throwBizException("10100005");
        }
        return StringUtils.join(goodAtList.toArray(), ",");
    }

    public static Doctor buildSaveDoctorGoodAt(String goodAt, Doctor doctor) {
        doctor.setGoodAt(goodAt);
        return doctor;
    }

    public static UserProfileSummaryDataVO buildUserProfileSummaryData(int favorCount, int diagnosisEvaluationCount, int patientNum) {
        UserProfileSummaryDataVO userProfileSummaryDataVO = new UserProfileSummaryDataVO();
        userProfileSummaryDataVO.setFavorCount(favorCount);
        userProfileSummaryDataVO.setDiagnosisEvaluationCount(diagnosisEvaluationCount);
        userProfileSummaryDataVO.setPatientNum(patientNum);
        return userProfileSummaryDataVO;
    }
}
